﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace NJCORS.forms
{
    public partial class ManageInspectUserInfo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
            diverrormsg.Style.Add("display", "none");
        }

        protected void griddata_RowEditing(object sender, GridViewEditEventArgs e)
        {
            griddata.EditIndex = e.NewEditIndex;
            BindData();
        }

        protected void griddata_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            try
            {
                string strInspectUserName = ((TextBox)(griddata.Rows[e.RowIndex].FindControl("txbInspectUserName"))).Text.ToString().Trim();
                string strInspectUserPassword = ((TextBox)(griddata.Rows[e.RowIndex].FindControl("txbInspectUserPassword"))).Text.ToString().Trim();
                string strInspectUserScope = ((TextBox)(griddata.Rows[e.RowIndex].FindControl("txbInspectUserScope"))).Text.ToString().Trim();
                if (strInspectUserName.Length < 1)
                {
                    throw new Exception("请输入用户名称！");
                }
                int cnt = -1;
                cnt = Convert.ToInt32(griddata.DataKeys[e.RowIndex].Value);
                if (cnt > 0)
                {
                    BLL.InspectUserInfo biui = new BLL.InspectUserInfo();
                    Model.InspectUserInfo miui = new Model.InspectUserInfo();
                    miui = biui.GetModel(cnt);
                    if (miui.InspectUserName.CompareTo(strInspectUserName) != 0)
                    {
                        //DataSet ds = biui.GetList("Enabled=1 and InspectUserName='" + strInspectUserName + "'");
                        if (biui.Exists(strInspectUserName))//(ds.Tables[0].Rows.Count > 0)//
                        {
                            throw new Exception("用户名称已存在！");
                        }
                        miui.InspectUserName = strInspectUserName;
                    }
                    miui.InspectUserPassword = strInspectUserPassword;
                    miui.InspectUserScope = strInspectUserScope;
                    miui.Enabled = true;
                    
                    griddata.EditIndex = -1;
                    biui.Update(miui);
                    BindData();
                }
                else if (cnt==0)
                {
                    Model.InspectUserInfo modelattribute = new Model.InspectUserInfo();
                    modelattribute.InspectUserID = cnt;
                    modelattribute.InspectUserName = strInspectUserName;
                    modelattribute.InspectUserPassword = strInspectUserPassword;
                    modelattribute.InspectUserScope = strInspectUserScope;
                    modelattribute.Enabled = true;
                    BLL.InspectUserInfo bllattribute = new BLL.InspectUserInfo();
                    //DataSet ds = bllattribute.GetList("Enabled=1 and InspectUserName='" + strInspectUserName + "'");
                    if (bllattribute.Exists(strInspectUserName))//(ds.Tables[0].Rows.Count > 0)//
                    {
                        throw new Exception("用户名称已存在！");
                    }
                    griddata.EditIndex = -1;
                    bllattribute.Add(modelattribute);
                    BindData();
                }
            }
            catch (System.Exception ex)
            {
                ShowMessage(false, ex.Message);
            }
        }

        protected void BindData()
        {
            string strsql = "select * from InspectUserInfo where Enabled=1";
            DataSet ds = DBUtility.DBHelperSQL.GetDataSet(strsql);
            griddata.DataSource = ds;
            griddata.DataKeyNames = new string[] { "InspectUserID" };
            griddata.DataBind();
            GlobalMethods.SetGridViewWidth(griddata, true);
        }

        protected void griddata_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onclick", "click2(this)");
                if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
                {
                    ((LinkButton)e.Row.FindControl("btndelete")).Attributes.Add("onclick",
                        "javascript:return confirm('你确认要删除用户\"" + ((Label)e.Row.FindControl("lbInspectUserName")).Text + "\"吗?')");
                }

            }
        }

        protected void griddata_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            griddata.EditIndex = -1;
            BindData();
        }

        protected void griddata_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            griddata.PageIndex = e.NewPageIndex;
            BindData();
        }

        protected void griddata_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                int cnt = -1;
                cnt = Convert.ToInt32(griddata.DataKeys[e.RowIndex].Value);
                if (cnt > 0)
                {
                    string strsql = "update InspectUserInfo set Enabled=0 where InspectUserID=" + cnt.ToString();
                    DBUtility.DBHelperSQL.GetNums(strsql);
                    BindData();
                }
            }
            catch (Exception ex)
            {
                ShowMessage(false, ex.Message);
            }
        }

        protected void AddRecord_Click(object sender, EventArgs e)
        {
            try
            {
                BindData();
                DataTable dt=new DataTable();
                InitDataTable(dt);
                ReadGridView(dt);
                AddRow(dt);
                WriteGridview(dt);
                int cnt = griddata.Rows.Count;
                griddata.EditIndex = griddata.Rows.Count-1;
                WriteGridview(dt);                
            }
            catch (System.Exception ex)
            {
                ShowMessage(false, ex.Message);
            }
        }

        protected void InitDataTable(DataTable dt)
        {
            dt.Clear();
            dt.Columns.Add("InspectUserID");
            dt.Columns.Add("InspectUserName");
            dt.Columns.Add("InspectUserPassword");
            dt.Columns.Add("InspectUserScope");
        }

        protected void ReadGridView(DataTable dt)
        {
            int i=0;
            for(i=0;i<griddata.Rows.Count;i++)
            {
                GridViewRow gRow = griddata.Rows[i];
                DataRow newRow = dt.NewRow();
                newRow[0] = griddata.DataKeys[i].Value;
                if (gRow.FindControl("lbInspectUserName") != null)
                {
                    newRow[1] = ((Label)gRow.FindControl("lbInspectUserName")).Text;
                    newRow[2] = ((Label)gRow.FindControl("lbInspectUserPassword")).Text;
                    newRow[3] = ((Label)gRow.FindControl("lbInspectUserScope")).Text;
                }
                dt.Rows.Add(newRow);
            }
            dt.AcceptChanges();
        }

        protected void AddRow(DataTable dt)
        {
            DataRow newRow = dt.NewRow();
            newRow[0] = "0";
            dt.Rows.Add(newRow);
            dt.AcceptChanges();
        }

        protected void WriteGridview(DataTable dt)
        {
            griddata.DataSource = dt;
            griddata.DataKeyNames = new string[] { "InspectUserID" };
            griddata.DataBind();
        }

        protected void ShowMessage(bool issuccessful, string message)
        {
            diverrormsg.Style.Add("display", "block");
            if (issuccessful == true)
            {
                diverrormsg.Style.Add("background-color", "#CCFF99");
                diverrormsg.Style.Add("border-color", "#009900");
                diverrormsg.Style.Add("color", "#006600");
                diverrormsg.Style.Add("background-image", "url(../themes/images/successful.gif)");
                lberrormsg.Text = message;
            }
            else
            {
                diverrormsg.Style.Add("background-color", "#ffebe8");
                diverrormsg.Style.Add("border-color", "#dd3c10");
                diverrormsg.Style.Add("color", "#dd3c10");
                diverrormsg.Style.Add("background-image", "url(../themes/images/error.gif)");
                lberrormsg.Text = message;
            }
        }
    }
}
